<?php

/**
 * 接口返回
 * @param	number	$id	参数id
 */
if (!function_exists('jsonReturn')) {
    function jsonReturn($code=200, $msg='', $data = null){
        $arr = [
            'code'=>$code,
            'msg'=>$msg,
        ];
        if($data) $arr['data'] = $data;
        return json_encode($arr,JSON_UNESCAPED_UNICODE);
    }
}

if (!function_exists('orderCreate')) {
    function orderCreate(){
        $code = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
//        $osn = $code[intval(date('Y')) - 2021] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        $osn = 'W'.intval(date('Y')) . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -3) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));

        return $osn;
    }
}

if (!function_exists('orderRefundCreate')) {
    function orderRefundCreate(){
//        $osn = $code[intval(date('Y')) - 2021] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        $osn = 'R'.intval(date('Y')) . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -3) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));

        return $osn;
    }
}


if(!function_exists('getTree')){
    function getTree($items,$pid = "parent_id"){
        $map  = [];
        $tree = [];
        foreach ($items as &$it){
            $map[$it['id']] = &$it;
        }  //数据的ID名生成新的引用索引树
        foreach ($items as &$at){
            $parent = &$map[$at[$pid]];
            if($parent) {
                $parent['children'][] = &$at;
            }else{
                $tree[] = &$at;
            }
        }
        return $tree;
    }
}
if(!function_exists('getSubs')){
    function getSubs($id = 0 ,$items){
        $returnData = array();
        foreach($items as $k => $v){
            if($v['parent_id'] == $id){
                $returnData[]		= $v;
                $a	= getSubs($v['id'], $items);
                if($a){
                    $returnData	= array_merge($returnData, $a);
                }
            }
        }
        return $returnData;
    }
}



if (!function_exists('setToken')) {
    function setToken($slug = 'member')
    {
        $str = mt_rand(1000,9999);
        return md5($slug.$str);
    }

}


/**
 * @param $url 请求网址
 * @param bool $params 请求参数
 * @param int $ispost 请求方式
 * @param int $https https协议
 * @return bool|mixed
 */

if (!function_exists('curlPost')) {
    function curlPost($url, $params = false, $ispost = 0, $https = 0)
    {
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($https) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
        }
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                if (is_array($params)) {
                    $params = http_build_query($params);
                }
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }

        $response = curl_exec($ch);

        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }
}




